<template>
  <Panel>
    <template #title>商品列表</template>
    <template>
      <!-- 1.0 表格渲染数据 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID">
                <span>{{ row.id }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ row.name }}</span>
              </el-form-item>
              <el-form-item label="所属分类">
                <span>{{ row.category }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ row.price }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ row.ctime | dateFormat }}</span>
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品销量">
                <span>{{ row.sellCount }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" width="210" prop="name">
        </el-table-column>
        <el-table-column label="所属分类" width="140" prop="category">
        </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片">
          <template v-slot="{ row }">
            <img :src="base_url + row.imgUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="商品描述" width="251" prop="goodsDesc">
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template>
            <el-button size="mini" type="primary">编辑</el-button>
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.0 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
  </Panel>
</template>

<script>
import { goodsList } from '@/api/goods'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      base_url: 'http://127.0.0.1:5000/upload/imgs/goods_img/'
    }
  },
  created() {
    this.getDatas()
  },
  methods: {
    async getDatas() {
      const { data, total } = await goodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      console.log(data)
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getDatas()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getDatas()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 40px;
}

img {
  width: 50px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
